package com.emep.changzhi.analyse.service.aqi;


import com.emep.changzhi.analyse.entity.aqi.AqiIactorInfo;
import com.emep.changzhi.analyse.repository.aqi.AqiIactorInfoRepository;
import com.emep.changzhi.analyse.utils.DateTimeUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sound.midi.SysexMessage;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.util.*;

/**
 * @author xy
 * @Description: 城市排名业务层
 * @date 2018/3/29 10:34
 * @version V1.0
 */

@Slf4j
@Data
@Service
@Transactional
public class AqiIactorInfoService {
    @Resource
    AqiIactorInfoRepository aqiIactorInfoRepository;
    public List<Map<String,Object>> list(String area_name){
        Date date_hour = DateTimeUtil.mathHourOfDate(new Date(),-1);
        String date_new = DateTimeUtil.getDateTimeForString(date_hour, "yyyy-MM-dd HH:") + "00:00";
        Date date = null;
        try {
           date = DateTimeUtil.stringToDate(date_new,DateTimeUtil.DATETIME_DEFAULT_FORMAT);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        List<AqiIactorInfo> list_realtime = aqiIactorInfoRepository.findByAreaAndTimePoint(area_name,date.getTime()+"");
        map.put("realTime",list_realtime);
        Date date_lastMonth = DateTimeUtil.mathMothOfDate(date,-1);
        List<AqiIactorInfo> list_oneMonth = aqiIactorInfoRepository.findByAreaAndTimePoint(area_name,date_lastMonth.getTime()+"");
        map.put("lastMonth",list_oneMonth);
        Date date_lastYear = DateTimeUtil.mathYearOfDate(date,-1);
        List<AqiIactorInfo> list_oneYear = aqiIactorInfoRepository.findByAreaAndTimePoint(area_name,date_lastYear.getTime()+"");
        map.put("lastYear",list_oneYear);
        list.add(map);
        return list;
    }
}
